package com.zry.fertilizer.presenter

import android.app.FragmentTransaction
import android.content.Intent
import android.view.View
import com.zry.fertilizer.MainActivity
import com.zry.fertilizer.R
import com.zry.fertilizer.been.HttpResponse
import com.zry.fertilizer.been.LoginInfoBean
import com.zry.fertilizer.retrofit.HttpServer
import com.zry.fertilizer.retrofit.ShowUserView
import com.zry.fertilizer.ui.activity.LoginActivity
import com.zry.fertilizer.ui.fragment.HomeFragment
import com.zry.fertilizer.ui.fragment.MessageFragment
import com.zry.fertilizer.ui.fragment.UserFragment
import com.zry.fertilizer.utils.ACacheUtil
import com.zry.fertilizer.utils.ToastUtil

class MainPresenter(private val activity: MainActivity)  {


    fun mainHandlerClick(view: View) {
        goneFragment()
        selectStyle(view.id)
    }


    fun selectStyle(linearId: Int) {
        val fragmentTransaction: FragmentTransaction = activity.fragmentManager.beginTransaction()
        when (linearId) {
            R.id.mainLin01 -> {
                if (activity.homeFragment == null) {
                    activity.homeFragment = HomeFragment()
                    fragmentTransaction.add(R.id.mainViewPage, activity.homeFragment)
                } else {
                    fragmentTransaction.show(activity.homeFragment)
                }
                //提交
                fragmentTransaction.commit()
            }
            R.id.mainLin02 -> {

                if (activity.messageFragment == null) {
                    activity.messageFragment = MessageFragment()
                    fragmentTransaction.add(R.id.mainViewPage, activity.messageFragment)
                } else {
                    fragmentTransaction.show(activity.messageFragment)
                }
                //提交
                fragmentTransaction.commit()
            }
            R.id.mainLin03 -> {

                if (activity.userFragment == null) {
                    activity.userFragment = UserFragment()
                    fragmentTransaction.add(R.id.mainViewPage, activity.userFragment)
                } else {
                    fragmentTransaction.show(activity.userFragment)
                }
                //提交
                fragmentTransaction.commit()
            }
        }
    }

    /**
     * 隐藏所有fragment的方法
     */
    private fun goneFragment() {
        val fragmentTransaction = activity.fragmentManager.beginTransaction()
        if (activity.homeFragment != null) {
            fragmentTransaction.hide(activity.homeFragment)
        }
        if (activity.messageFragment != null) {
            fragmentTransaction.hide(activity.messageFragment)
        }
        if (activity.userFragment != null) {
            fragmentTransaction.hide(activity.userFragment)
        }
        fragmentTransaction.commit()
    }
}